package com.mapforge.selection;

import java.awt.Rectangle;

public class ComparableRectangle extends Rectangle implements
		Comparable<ComparableRectangle> {
	/**
	 * Default serial version ID to remove warnings. This class is not
	 * serializable.
	 */
	private static final long serialVersionUID = 1L;

	public ComparableRectangle() {
		this(0, 0, 0, 0);
	}

	public ComparableRectangle(Rectangle r) {
		this(r.x, r.y, r.width, r.height);
	}

	public ComparableRectangle(int x, int y, int width, int height) {
		super(x, y, width, height);
	}

	@Override
	public int compareTo(ComparableRectangle rect) {
		if (rect != null) {
			if (rect.intersects(this)) {
				throw new IllegalArgumentException(
						"Comparable Rectangles cannot intersect.");
			}
			if (rect.x == this.x && rect.y == this.y) {
				return 0;
			} else if (rect.y == this.y && rect.x != this.x) {
				return this.x < rect.x ? -1 : 1;
			} else {
				return this.y < rect.y ? -1 : 1;
			}
		} else {
			return 1;
		}
	}

	public String toString() {
		return "[" + x + ", " + y + ", " + width + ", " + height + "]";
	}

	public boolean equals(Object obj) {
		if (obj != null && obj instanceof Rectangle) {
			Rectangle r = (Rectangle) obj;
			return r.x == this.x && r.y == this.y && r.width == this.width
					&& r.height == this.height;
		} else {
			return false;
		}
	}
}
